package com.hm.service.impl.task.help;

import org.quartz.*;

public class TaskUtils {

    /**
     * 构建 JobDetail
     *
     * @param jobKey   JobKey
     * @param jobClass jobClass
     * @return JobDetail
     */
    public static JobDetail generateJobDetail(JobKey jobKey, Class<? extends Job> jobClass) {
        return JobBuilder.newJob(jobClass)//
                .withIdentity(jobKey)//
                .build();
    }

    /**
     * 构建Trigger
     *
     * @param triggerKey TriggerKey
     * @param cron       cron
     * @return Trigger
     */
    public static Trigger generateTrigger(TriggerKey triggerKey, String cron) {
        return TriggerBuilder.newTrigger()//
                .withIdentity(triggerKey)//
                .withSchedule(CronScheduleBuilder.cronSchedule(cron))//
                //极短的时间间隔内，Quartz调度器可能会多次检查并执行与触发器相关联的作业会触发多次
                // 10秒后生效，要不然调度启动后如果延迟，可能出现首次执行多遍job
                .startAt(DateBuilder.futureDate(10, DateBuilder.IntervalUnit.SECOND))//
                .build();
    }
}
